﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management;

namespace PMS.Utils
{
    public class Register
    {
        RegisterInfo m_info;
        private static string FileName = "UInfo.dat";
        private static string folderName = "PMS";
        public Register()
        {
            m_info = new RegisterInfo();
        }

        public static string GetFileName(string folder, string file)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            if (!Directory.Exists(path + @"\" + folder))
            {
                Directory.CreateDirectory(path + @"\" + folder);
            }

            if (!File.Exists(path + @"\" + folder + @"\" + file))
            {
                File.Create(path + @"\" + folder + @"\" + file);
            }

            return path + @"\" + folder + @"\" + file;
        }
        public Register(RegisterInfo info)
        {
            m_info = info;
        }

        public static bool IsValidKey(string key)
        {

            if (GetProcessorID().Equals(CryptorEngine.Decrypt(key, true), StringComparison.CurrentCultureIgnoreCase))
            {
                return true;
            }
            return false;
        }

        public static void WriteRegisterInfoToFile(RegisterInfo info)
        {

            string path = GetFileName(folderName, FileName);
            StreamWriter fileWrite = new StreamWriter(path);
            try
            {

                fileWrite.WriteLine(CryptorEngine.Encrypt(CryptorEngine.Encrypt(info.Fullname, true), true));
                fileWrite.WriteLine(CryptorEngine.Encrypt(CryptorEngine.Encrypt(info.CompanyName, true), true));
                fileWrite.WriteLine(CryptorEngine.Encrypt(CryptorEngine.Encrypt(info.Key, true), true));

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                fileWrite.Close();
            }
        }

        public static string GetProcessorID()
        {
            string sProcessorID = "";
            string sQuery = "SELECT ProcessorId FROM Win32_Processor";

            ManagementObjectSearcher oManagementObjectSearcher = new ManagementObjectSearcher(sQuery);

            ManagementObjectCollection oCollection = oManagementObjectSearcher.Get();

            foreach (ManagementObject oManagementObject in oCollection)
            {

                sProcessorID = (string)oManagementObject["ProcessorId"];

            }

            return (sProcessorID);

        }

        public static RegisterInfo ReadRegisterInfoFromFile()
        {
            try
            {
                string path = GetFileName(folderName, FileName);
                if (File.Exists(path))
                {
                    RegisterInfo info = new RegisterInfo();
                    StreamReader fileRead = new StreamReader(path);
                    info.Fullname = CryptorEngine.Decrypt(CryptorEngine.Decrypt(fileRead.ReadLine(), true), true);
                    info.CompanyName = CryptorEngine.Decrypt(CryptorEngine.Decrypt(fileRead.ReadLine(), true), true);
                    info.Key = CryptorEngine.Decrypt(CryptorEngine.Decrypt(fileRead.ReadLine(), true), true);
                    fileRead.Close();
                    return info;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
            }
            return null;
        }
    }

    public class RegisterInfo
    {
        string m_Fullname;
        string m_CompanyName;
        string m_Key;

        public string Fullname
        {
            get { return m_Fullname; }
            set { m_Fullname = value; }
        }

        public string CompanyName
        {
            get { return m_CompanyName; }
            set { m_CompanyName = value; }
        }

        public string Key
        {
            get { return m_Key; }
            set { m_Key = value; }
        }

        public RegisterInfo()
        {
            m_Fullname = "";
            m_CompanyName = "";
            m_Key = "";
        }

        public RegisterInfo(string name, string company, string key)
        {
            m_Fullname = name;
            m_CompanyName = company;
            m_Key = key;
        }


    }
}
